var util = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
var pickerFile = require('../../wxParse/picker_datetime.js');
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hunterInfo:{},
    hunterid:0,
    itemid:0,
    proId:null,
    proType:null,
    proTime:null,
    pay:"",
    resTotal:0.00,
    voucherMoney:0.00,//优惠券总额
    couponList:{},
    iteminfo:{},
    proTimeInfo:{},
    userstartDate: "请选择预约开始时间",
    date: '',
    time: '', 
    orderformId:''
  },
  //  点击时间组件确定事件  
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  //  点击日期组件确定事件  
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    var date=new Date();
    var month = date.getMonth()+1;
    var dateStr = date.getFullYear() + "-" + month + "-" + date.getDate();
    var timeStr = date.getHours() + ":" + date.getMinutes();
    that.setData({
      hunterInfo: JSON.parse(options.hunter),
      proId: options.proId,
      proType: options.proType,
      proTime: options.proTime,
      date: dateStr,
      time: timeStr,
    });

    //已选择的咨询项目
    var itemid = options.proId;
    util.post(app.api.getOrderDescId, { oritemid: itemid }, function (res) {
      console.log("已选的咨询项目"+res.data);
      that.setData({
        iteminfo: res.data,
        resTotal: res.data.spec
      });
    });

    //已选择得预约时间
    util.post(app.api.getOrderTimeId, { proTimeId: options.proTime }, function (res) {
      console.log("已选的咨询时间" + res.data);
      that.setData({
        proTimeInfo: res.data
      });
    });

    //选择优惠劵
    var unionid = app.globalData.unionid;
    util.post(app.api.getUserCouponList, { unionid: unionid, spec: 0}, function (res) {
      console.log(res.data);
      that.setData({
        couponList: res.data
      });
    });
  },
  couponTap:function(){
    var that=this;
    var spec = that.data.iteminfo.spec;
    wx.navigateTo({
      url: '../my-coupon/my-coupon?spec=' + spec
    });
  },
  // 预约时间获取焦点
  startDatefocus: function (e) {
    this.setData({
      userstartDate: ""
    })
  },
  //预约时间失去焦点
  startDateblur: function (e) {
    this.setData({
      userstartDate: "请选择预约开始时间",
      startDate: e.detail.value
    })
  },
  //关闭成功提示
  closeTap: function () {
    this.setData({
      mode: true,
      height: "100%"
    })
  },
  
  //分享
  onShareAppMessage: function (res) {
    
  },
  submitOrder: function (e) {
    var that = this;
    var formId = e.detail.formId;
    var order_time = that.data.proTimeInfo.date + " " + that.data.proTimeInfo.startTime + "-" + that.data.proTimeInfo.endTime;
    var proType = that.data.proType;
    wx.checkSession({
      success: function () {
        //session 未过期，并且在本生命周期一直有效
        var tpcode = app.getWxCode();
        var tpid = app.getOpenId();
        //为保险起见，同时判断code跟openid是否有空值

        if (tpcode == null || tpcode.length < 1 || tpid == null || tpid.length < 1) {
          //本地信息为空，重新登陆
          app.login(function (openid, userInfo, wxcode) {
            console.log(openid);
            util.wxpay(app.api.wxpay, wxcode, openid, that.data.resTotal, that.data.hunterInfo.id, that.data.proId, order_time, proType, formId);
          });
        } else {
          //本地用户信息有效
          util.wxpay(app.api.wxpay, tpcode, tpid, that.data.resTotal, that.data.hunterInfo.id, that.data.proId, order_time, proType, formId);
        }

      },
      fail: function () {
        //登录态过期 重新登录
        app.login(function (openid, userInfo, wxcode) {
          util.wxpay(app.api.wxpay, wxcode, openid, that.data.resTotal, that.data.hunterInfo.id, that.data.proId, order_time, proType, formId);
        });
      }
    })
  },
  formSubmit: function (e) {
    var formId = e.detail.formId;
    this.setData({
      orderformId:formId
    })
  },

})